package cn.tellsea.mq.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 简单模型-simple
 *
 * @author Tellsea
 * @date 2023/9/5
 */
@Component
public class SimpleListener {

    @RabbitListener(queuesToDeclare = @Queue("spring.simple.queue"))
    public void listener(Message msg, Channel channel) throws IOException {

        String correlationId = (String) msg.getMessageProperties().getHeaders().get("spring_returned_message_correlation");

        String messageId = msg.getMessageProperties().getMessageId();

        System.out.println("消息标识ID：" + correlationId);

        System.out.println("SimpleListener listener 接收到消息：" + msg.getBody());
        // 确认消息
        // 参数1：该消息的index
        // 参数2：是否批量将一次性确认所有小于deliveryTag的消息
        // channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);

        // 拒绝确认
        // 参数1：该消息的index
        // 参数2：是否批量将一次性拒绝所有小于deliveryTag的消息
        // 参数3：被拒绝的是否重新入队列
        // channel.basicNack(msg.getMessageProperties().getDeliveryTag(), false, false);

        throw new RuntimeException("消息重试！");
    }
}
